﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SisLegal.Model;

namespace SisLegal.ExpedientFiled
{
    public partial class ExpedientFiledEditForm : System.Web.UI.Page
    {
        private int ID
        {
            get
            {
                if (Request.QueryString["id"] == null)
                    return 0;
                else
                    return int.Parse(Request.QueryString["id"]);
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {                
                DDL_Type_Process.DataSource = Enumeration.GetAll<eTypeProcess>();
                DDL_Type_Process.DataTextField = "Value";
                DDL_Type_Process.DataValueField = "Key";
                DDL_Type_Process.DataBind();

                DDL_ResultSentences.DataSource = Enumeration.GetAll<eResultSentences>();
                DDL_ResultSentences.DataTextField = "Value";
                DDL_ResultSentences.DataValueField = "Key";
                DDL_ResultSentences.DataBind();

                DDL_Participation_Seal.DataSource = Enumeration.GetAll<eParticipation_Seal_Fiscal_Folder>();
                DDL_Participation_Seal.DataTextField = "Value";
                DDL_Participation_Seal.DataValueField = "Key";
                DDL_Participation_Seal.DataBind();
                
                //---------------------------------------------------------
                Model.ExpedientFiledManager manager = new Model.ExpedientFiledManager();
                Model.ExpedientFiled obj = manager.GetById(ID);

                this.txtNroExpedient.Text = obj.NroExpedient;               
                DDL_Type_Process.ClearSelection();
                DDL_Type_Process.Items.FindByValue(obj.TypeProcess.ToString()).Selected = true;                

                Model.ViaProceduralManager ViaProcedural = new Model.ViaProceduralManager();
                ddlViaProcedural.DataSource = ViaProcedural.GetByProcessID(Convert.ToInt32(DDL_Type_Process.SelectedItem.Value));
                ddlViaProcedural.DataTextField = "Name";
                ddlViaProcedural.DataValueField = "ID";
                ddlViaProcedural.DataBind();

                ddlViaProcedural.ClearSelection();
                ddlViaProcedural.Items.FindByValue(obj.ViaProcedural.ToString()).Selected = true;  
                
                this.txtDateBegin.Text = String.Format("{0:dd/MM/yyyy}", obj.DateBegin);
                this.txtDateEnd.Text = String.Format("{0:dd/MM/yyyy}", obj.DateEnd);
                                
                DDL_ResultSentences.ClearSelection();
                DDL_ResultSentences.Items.FindByValue(obj.ResultSentences.ToString()).Selected = true;  

                this.txtJuzgado.Text = obj.Juzgado;
                this.txtPretencion.Text = obj.Pretencion;
                this.txtDemandante.Text = obj.Demandante;
                this.txtDemandado.Text = obj.Demandado;

                DDL_Participation_Seal.ClearSelection();
                DDL_Participation_Seal.Items.FindByValue(obj.ParticipationSeal.ToString()).Selected = true; 
                                
                this.txtLawyerOther.Text = obj.LawyerOther;
                this.txtSumilla.Text = obj.Sumilla;
                this.txtObservaciones.Text = obj.Observaciones;
                

            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            this.Error.Text = "Error : <br>";
            if (this.txtNroExpedient.Text == "")
            {
                this.Error.Text = " * Ingrese Numero de Expediente <br>";
                this.txtNroExpedient.Focus();
                return;
            }

            Model.ExpedientFiledManager manager = new Model.ExpedientFiledManager();
            Model.ExpedientFiled obj = new Model.ExpedientFiled();

            if (txtDateBegin.Text == "") { txtDateBegin.Text = "0001-01-01"; }
            if (txtDateEnd.Text == "") { txtDateEnd.Text = "0001-01-01"; }

            obj.NroExpedient = this.txtNroExpedient.Text.Trim();
            obj.TypeProcess = Convert.ToInt32(this.DDL_Type_Process.SelectedItem.Value);
            obj.ViaProcedural = Convert.ToInt32(this.ddlViaProcedural.SelectedItem.Value);
            obj.DateBegin = Convert.ToDateTime(this.txtDateBegin.Text);
            obj.DateEnd = Convert.ToDateTime(this.txtDateEnd.Text);
            obj.ResultSentences = Convert.ToInt32(this.DDL_ResultSentences.SelectedItem.Value);
            obj.Juzgado = this.txtJuzgado.Text.Trim();
            obj.Pretencion = this.txtPretencion.Text.Trim();
            obj.Demandante = this.txtDemandante.Text.Trim();
            obj.Demandado = this.txtDemandado.Text.Trim();
            obj.ParticipationSeal = Convert.ToInt32(this.DDL_Participation_Seal.SelectedItem.Value);
            obj.LawyerOther = this.txtLawyerOther.Text.Trim();
            obj.Sumilla = this.txtSumilla.Text.Trim();
            obj.Observaciones = this.txtObservaciones.Text.Trim();
            obj.ID = ID;

            manager.Update(obj);

            Response.Redirect("~/ExpedientFiled/ExpedientFiledListView.aspx");

        }

        protected void Index_Changed(Object sender, EventArgs e)
        {
            Model.ViaProceduralManager manager = new Model.ViaProceduralManager();
            ddlViaProcedural.DataSource = manager.GetByProcessID(Convert.ToInt32(DDL_Type_Process.SelectedItem.Value));
            ddlViaProcedural.DataTextField = "Name";
            ddlViaProcedural.DataValueField = "ID";
            ddlViaProcedural.DataBind();
        } 
        
    }
}